package com.rabiitmq.spring.ack;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.UUID;

/**
 * @author xiaochi
 * @date 2022/4/9 15:41
 * @desc TestProvider（生产者 ack）
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestProviderController {

    private ObjectMapper mapper = new ObjectMapper();

    @Value("${mq.config.exchange}")
    private String exchange;
    @Autowired
    private MqService mqService;

    @GetMapping("/send3/{msg}")
    public String send3(@PathVariable String msg) throws Exception {
        HashMap<String, String> map = new HashMap<>();
        map.put("id",""+Math.random());
        map.put("name",msg);
        mqService.sendMessage(exchange,"a.b",map);
        return "ok";
    }

    //-----------------------------------------------------------------------------------------

    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send/{msg}")
    public String send(@PathVariable String msg) throws Exception {
        rabbitTemplate.setConfirmCallback((correlationData,ack,cause) -> {
            if (!ack){
                log.info("消息发送失败:,{}", cause);
            }else{
                log.info("消息发送成功:,{}", correlationData);
            }
        });
        rabbitTemplate.setReturnCallback((message, replyCode,replyText, exchange, routingKey) -> {
            log.info("消息主体:,{}", message);
            log.info("应答码:,{}", replyCode);
            log.info("描述:,{}", replyText);
            log.info("消息使用的交换器 exchange :,{}", exchange);
            log.info("消息使用的路由键 routing :,{}", routingKey);
        });

        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        /*TestPojo testPojo = new TestPojo();
        testPojo.setId(""+Math.random());
        testPojo.setName(msg);
        // Message message = MessageBuilder.withBody(msg.getBytes()).build(); // 不是持久化消息
        Message message = MessageBuilder.withBody(msg.getBytes())
                .setDeliveryMode(MessageDeliveryMode.PERSISTENT) // 消息持久化
                .build();
        rabbitTemplate.convertAndSend(exchange,"a.b",mapper.writeValueAsString(testPojo),correlationData);*/

        /**/
        HashMap<String, String> map = new HashMap<>();
        map.put("id",""+Math.random());
        map.put("name",msg);
        rabbitTemplate.convertAndSend(exchange,"c.d",map,correlationData);
        return "ok";
    }

    @GetMapping("/send1/{msg}")
    public String send1(@PathVariable String msg){
        Message message = MessageBuilder.withBody(msg.getBytes()).build();
        amqpTemplate.convertAndSend(exchange,"a.b",message);
        return "ok";
    }
}
